/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.dialogs;

import java.awt.GridLayout;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;

import dgbgui.modeldocument.AttributePrototype;
import dgbgui.modeldocument.ModelDocument;
import dgbgui.modeldocument.ModelDrivenWorkspace;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.definition.FilteredInstanceDefinition;
import dgbgui.modeldocument.primitives.Concept;
import dgbgui.modeldocument.primitives.ConceptType;
import dgbgui.modeldocument.uimodels.ContainsEqualComboModel;
import dgbgui.tools.DialogedTool;

@SuppressWarnings("serial")
public class ModelDrivenFindDialog extends DGBGUIDialog  {
    
    public ModelDrivenFindDialog(Concept concept, DialogedTool tool) {
        super(tool,"Find items");

        setLayout(new BoxLayout(getContentPane(),BoxLayout.Y_AXIS));

        m_textFieldMap = new HashMap<String,JTextField>();
        m_comboModelMap = new HashMap<String,ContainsEqualComboModel>();

        ConceptType type = (ConceptType)ModelDocument.getInstance().getConceptronType(concept);
        Set<String> attribNames = ModelDocument.getInstance().getAttributeNames(type);
        FilteredInstanceDefinition def = null;
        if ( ModelDrivenWorkspace.getInstance().getQuery(concept) != null ) {
           // The definition must be an instance filter
            Query query = ModelDrivenWorkspace.getInstance().getQuery(concept);
            assert ( query.getDefinition() instanceof FilteredInstanceDefinition );
            def = (FilteredInstanceDefinition)query.getDefinition();
        }
        

        int numRows = attribNames.size();
        JPanel attributePanel = new JPanel(new GridLayout(numRows,3));
        Border panelBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
        attributePanel.setBorder(BorderFactory.createTitledBorder(panelBorder,"Attribute filters"));

        for ( String name : attribNames ) {
            JLabel nameLabel = new JLabel(name);

            ContainsEqualComboModel comboModel = new ContainsEqualComboModel();
            JTextField filter = new JTextField();
            JComboBox typeCombo = new JComboBox(comboModel);
            
            // If there is a definition, populate it with the current values
            if ( def != null ) {
                for ( AttributePrototype attribProto : def.getAttribProtoTypes() ) {
                    if ( name == attribProto.getName() ) {
                        if ( attribProto.getFilterType() == AttributePrototype.FilterType.EQUALS ) {
                            typeCombo.setSelectedItem("=");
                        } else {
                            typeCombo.setSelectedItem("contains");
                        }

                        // Set the value of the filter
                        filter.setText(attribProto.getFilterValue());
                    }
                }
            }

            m_textFieldMap.put(name,filter);
            m_comboModelMap.put(name,comboModel);

            attributePanel.add(nameLabel);
            attributePanel.add(typeCombo);
            attributePanel.add(filter);
        }
        add(attributePanel);
        
        
        // Add the OK and Cancel buttons
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new BoxLayout(buttonsPanel,BoxLayout.X_AXIS));
        m_okButton = new JButton("OK");

        m_cancelButton = new JButton("Cancel");
        buttonsPanel.add(m_okButton);
        buttonsPanel.add(m_cancelButton);

        m_okButton.addActionListener(tool);
        m_cancelButton.addActionListener(tool);

        add(buttonsPanel);
        
    }
    
    
    public String getAttributeFilterString(String name) {
        return m_textFieldMap.get(name).getText();
    }
    
    public AttributePrototype.FilterType getAttributeFilterType(String name) {
        return m_comboModelMap.get(name).getSelected();
    }
    
    
    private Map<String,JTextField> m_textFieldMap;
    private Map<String,ContainsEqualComboModel> m_comboModelMap;
    



}
